layui.use(['table','layer',"form"],function(){
    var layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        table = layui.table;

    // 订单列表展示
    var  tableIns = table.render({
        elem: '#orderDetailList',
        url : ctx+'/order_details/list?orderId='+$("input[name='id']").val(),
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 10,
        toolbar: "#toolbarDemo",
        id : "customerOrderListTable",
        cols : [[
            {type: "checkbox", fixed:"center"},
            {field: "id", title:'编号',fixed:"true"},
            {field: 'goodsName', title: '商品名称',align:"center"},
            {field: 'goodsNum', title: '商品数量',align:"center"},
            {field: 'unit', title: '单位',align:"center"},
            {field: 'price', title: '单价(￥)',align:"center"},
            {field: 'sum', title: '总价(￥)',align:"center"},
            {field: 'createDate', title: '创建时间',align:"center"},
            {field: 'updateDate', title: '更新时间',align:"center"},
            {title: '操作',fixed:"right",align:"center", minWidth:60,templet:"#orderDetailsListBar"}
        ]]
    });

    table.on('toolbar(orderDetails)', function (data) {
        if (data.event == "add") {
            // 添加操作 OrderDetails
            openAddOrUpdateOrderDetailsDialog();
        }
    })

    table.on('tool(orderDetails)',function (data) {
       if (data.event == "edit") {
            openAddOrUpdateOrderDetailsDialog(data.data.id)
        }else if (data.event == "del") {
            deleteOrderDetails(data.data.id);

        }
    });

    function openAddOrUpdateOrderDetailsDialog(id) {
        // 弹出层的标题
        var title = "<h3>订单明细管理 - 添加订单明细信息</h3>";
        var url = ctx + "/order_details/toAddOrUpdateOrderDetailsPage?oId="+$("input[name='id']").val();

        if (id != null && id != '') {
            title  = "<h3>订单明细管理 - 更新订单明细信息</h3>";
            url += '&id=' + id;
        }

        layui.layer.open({
            type: 2,
            title: title,
            area: ['500px', '420px'],
            content: url,
            maxmin:true
        });
    }

    function  deleteOrderDetails(id) {
        // 弹出确认框，询问用户是否确认删除
        layer.confirm('您确认要删除该记录吗？',{icon:3, title:'订单明细管理'}, function (index) {
            // 发送ajax请求，执行删除操作
            $.post(ctx + '/order_details/delete',{id:id}, function (result) {
                // 判断删除结果
                if (result.code == 200) {
                    // 提示成功
                    layer.msg('删除成功',{icon:6});
                    // 刷新数据表格
                    tableIns.reload();
                } else {
                    // 提示失败原因
                    layer.msg(result.msg,{icon:5});
                }
            });
        });
    }

});
